package at.codebase.db.entities;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Embed;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Load;

/**
 * 
 * @author Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
 *
 */
@Entity
public class EMessageExt implements IMessage {
	
	@Id
	private Long messageId;
	
	@Embed
	public static class UserExt {
		
		public UserExt(){ }
		
		public UserExt(Ref<EApiPartner> partner, String userId, String userName){
			this.partner = partner;
			this.userId = userId;
			this.userName = userName;
		}
		
		@Load
		Ref<EApiPartner> partner;
		String userId;
		String userName;
		
		public Ref<EApiPartner> getPartner() {
			return this.partner;
		}

		public String getUserId() {
			return this.userId;
		}

		public String getUserName() {
			return this.userName;
		}
	}
	
	@Index
	@Load
	private Ref<EUser> addressor;
	private UserExt addressorExt;
	
	@Index
	@Load
	private Ref<EUser> recipient;
	private UserExt recipientExt;
	
	private boolean pm; 		//true of its a normal PM
	@Load
	private String item;	//Null of its a normal PM
	
	private String text;
	
	private String conversationId;
	
	private long timeSend;	
	
	/*
	 * Objectify
	 */
	@SuppressWarnings("boxing")
	public static Key<EMessageExt> key(Long messageId) {
		return Key.create(EMessageExt.class, messageId);
	}
	
	public static Ref<EMessageExt> ref(Long messageId) {
		Key<EMessageExt> key = key(messageId);
		return Ref.create(key);
	}
	
	/*
	 * Constructors
	 */
	public EMessageExt(){ }
	
	public EMessageExt(boolean pm, long timeSend) {
		this.pm = pm;
		this.timeSend = timeSend;
	}
	
	public static EMessageExt createItemIncomingMessageExt(UserExt addressor, Ref<EUser> recipient, String item, String text, String conversationId){
		EMessageExt msg = new EMessageExt(false, System.currentTimeMillis());
		msg.addressorExt = addressor;
		msg.recipient = recipient;
		msg.item = item;
		msg.text = text;
		msg.conversationId = conversationId;
		return msg;
	}	
	
	public static EMessageExt createPrivateIncomingMessageExt(UserExt addressor, Ref<EUser> recipient, String text, String conversationId){
		EMessageExt msg = new EMessageExt(true, System.currentTimeMillis());
		msg.addressorExt = addressor;
		msg.recipient = recipient;
		msg.text = text;
		msg.conversationId = conversationId;
		return msg;
	}
	
	public static EMessageExt createItemOutgoingMessageExt(Ref<EUser> addressor, UserExt recipient, String item, String text, String conversationId){
		EMessageExt msg = new EMessageExt(false, System.currentTimeMillis());
		msg.addressor = addressor;
		msg.recipientExt = recipient;
		msg.item = item;
		msg.text = text;
		msg.conversationId = conversationId;
		return msg;
	}
	
	public static EMessageExt createPrivateOutgoingMessageExt(Ref<EUser> addressor, UserExt recipient, String text, String conversationId){
		EMessageExt msg = new EMessageExt(true, System.currentTimeMillis());
		msg.addressor = addressor;
		msg.recipientExt = recipient;
		msg.text = text;
		msg.conversationId = conversationId;
		return msg;
	}

	public Long getId() {
		return messageId;
	}

	public Ref<EUser> getAddressor() {
		return addressor;
	}

	public UserExt getAddressorExt() {
		return addressorExt;
	}

	public Ref<EUser> getRecipient() {
		return recipient;
	}

	public UserExt getRecipientExt() {
		return recipientExt;
	}

	public boolean isPm() {
		return pm;
	}

	public String getItem() {
		return item;
	}

	public String getText() {
		return text;
	}

	@Override
	public long getTimeSend() {
		return timeSend;
	}

	public String getConversationId() {
		return conversationId;
	}

	@Override
	public String toString() {
		return "EMessageExt [messageId=" + messageId + ", addressor=" + addressor + ", addressorExt=" + addressorExt
				+ ", recipient=" + recipient + ", recipientExt=" + recipientExt + ", pm=" + pm + ", item=" + item
				+ ", text=" + text + ", conversationId=" + conversationId + ", timeSend=" + timeSend + "]";
	}
	
}
